﻿using System;
using Microsoft.Practices.Unity;
using Gurock.SmartInspect;
using Microsoft.Practices.Unity.InterceptionExtension;
using System.Threading;

namespace SimpleExample
{
    class Program
    {
        /// <summary>
        ///     Main-Entry method for the example program.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            InitSmartInspect();

            IUnityContainer container = new UnityContainer();
            InitUnity(container);

            ISimpleLoggedClass simpleClass = container.Resolve<ISimpleLoggedClass>();

            // Simple method call
            simpleClass.SomeMethod();

            Console.WriteLine("Method was called, looping watches now.");

            // Do some loops for showing watch
            Random rand = new Random(DateTime.Now.Millisecond);
            for (int i = 100, l = 200; i < l; i++)
            {
                simpleClass.WatchProperty = rand.Next(i);
                Console.WriteLine("Loop .... ", DateTime.Now);
                Thread.Sleep(500);
            }
            Console.WriteLine("Loop done, press <ENTER>");
            Console.ReadLine();

            DisableSmartInspect();
        }

        /// <summary>
        ///     Initializing the UnityContainer and the needed Interceptions.
        /// </summary>
        /// <param name="container"></param>
        private static void InitUnity(IUnityContainer container)
        {
            container
                // add the interception extension to that container
                            .AddNewExtension<Interception>()
                // register our worker type
                            .RegisterType<ISimpleLoggedClass, SimpleLoggedClass>(); ;

            // configure our simple class
            container.Configure<Interception>()
                // so that it intercepts our worker
                .SetInterceptorFor<ISimpleLoggedClass>(new TransparentProxyInterceptor());
        }

        /// <summary>
        ///     Initializing the SmartInspect framework.
        /// </summary>
        private static void InitSmartInspect()
        {
            SiAuto.Si.Connections = "pipe()";
            SiAuto.Si.Enabled = true;
        }

        /// <summary>
        ///     Disables logging features.
        /// </summary>
        private static void DisableSmartInspect()
        {
            SiAuto.Si.Enabled = false;
        }
    }
}
